#!/bin/bash
#Author : Narendra Sisodiya
#Date : Sunday, September 20 2009
#Version : 1.0
#Email : narendra.sisodiya@gmail.com
#Webpage : http://narendra.techfandu.org

#TODO : just convert all <jpg> files from <fromdir> to <todir> into <png> format
#TODO : help function
#TODO : check for .tar.gz function
#TODO :	remove nested if else for command
#TODO : just search all pdf files
#TODO : remove a.out command may remove directory "a.out"
#TODO : use "delete" instead of remove

MOVE_OR_COPY=$1
COMMAND="null"

if [ "$MOVE_OR_COPY" != "move" ]
then
	if [ "$MOVE_OR_COPY" != "copy" ]
	then
		if [ "$MOVE_OR_COPY" != "remove" ]
		then
			echo "Syntax Error"
			echo "      :: move all <pdf> files from <fromdir> to <todir>"
			echo "      :: copy all <html> files from <fromdir> to <todir>"
			echo "      :: remove all <exe> files from <fromdir>"
			echo "   -- Execute with desired file extention"
			exit 1
		else
			COMMAND="rm"
		fi
	else
		COMMAND="cp"
	fi
else
COMMAND="mv"
fi

#all
if [ "$2" != "all" ]
then
echo "Syntax Error"
echo "      :: move all pdf files from <fromdir> to <todir>"
echo "      :: copy all pdf files from <fromdir> to <todir>"
exit 1
fi


#type
EXT=$3

#files
if [ "$4" != "files" ]
then
echo "Syntax Error"
echo "      :: move all pdf files from <fromdir> to <todir>"
echo "      :: copy all pdf files from <fromdir> to <todir>"
exit 1
fi

#from
if [ "$5" != "from" ]
then
echo "Syntax Error"
echo "      :: move all pdf files from <fromdir> to <todir>"
echo "      :: copy all pdf files from <fromdir> to <todir>"
exit 1
fi




#from dir
FROMDIR=$6

if [ $MOVE_OR_COPY != "remove" ]
then
	#to
	if [ "$7" != "to" ]
	then
		echo "Syntax Error"
		echo "      :: move all pdf files from <fromdir> to <todir>"
		echo "      :: copy all pdf files from <fromdir> to <todir>"
		exit 1
	fi

	#moving directory
	TODIR=$8
	
	mkdir -p $TODIR

	if [ $? -eq 1  ]
	then
		echo "Falied to created directory"
		exit 1
	else
		#find $FROMDIR -name "*$EXT" |while read Filename ; do echo $MOVE_OR_COPY"ing file - " $Filename; $COMMAND "$Filename" "$TODIR" ; done
	fi
else
		#find $FROMDIR -name "*$EXT" |while read Filename ; do echo $MOVE_OR_COPY"ing file - " $Filename; $COMMAND "$Filename"; done
fi

exit 0
